package com.tengju.data.infrastructure.repository;

import com.tengju.data.domain.model.motivation.DwsBonusTopYearRepository;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.data.custom.DwsBonusTopYearMapper;
import com.tengju.user.domain.model.Row;
import com.tengju.user.domain.model.user.UserIdCode;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class DwsBonusTopYearRepositoryImpl implements DwsBonusTopYearRepository {

    @Resource
    DwsBonusTopYearMapper dwsBonusTopYearMapper;


    /**
     * 年度收益个人排行
     */
    @Override
    public Row getSelfYearRank(UserIdCode userIdCode, Integer year,Integer lastYear, Integer topLimit) {
        Map<String,Object> map = dwsBonusTopYearMapper.getSelfYearRank(userIdCode.getValue(),
                year,
                lastYear,
                topLimit);

        return new Row(map);
    }

    /**
     * 年度收益总排行
     */
    @Override
    public List<Row> getAllYearRank(Integer year, Integer topLimit,Integer offset,Integer limit) {

        List<Map<String,Object>> mapList =
                dwsBonusTopYearMapper.getAllYearRank(year,topLimit,offset,limit);

        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> getByIdCodeAndYear(List<UserIdCode> idCodeList, Integer year, Integer topLimit) {
        if (CollectionUtils.isEmpty(idCodeList)) {
            return Collections.emptyList();
        }
        List<Long> userIdCodeList = idCodeList.stream()
                .filter(Objects::nonNull)
                .map(UserIdCode::getValue)
                .collect(Collectors.toList());
        List<Map<String,Object>> mapList =
                dwsBonusTopYearMapper.getByIdCodeAndYear(userIdCodeList,year,topLimit);
        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public Integer getTotal(Integer year, Integer topLimit) {
        return dwsBonusTopYearMapper.getTotal(year,topLimit);
    }


}
